package gtug.duesseldorf.client;

import gtug.duesseldorf.client.ui.DCListBox;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Wave_webclient implements EntryPoint {
	
	// TODO: move to config file
	private static String default_username = "bar@mydomain.local";
	
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		// define GUI
		
		FlexTable flexTable = new FlexTable();
		flexTable.setWidth("100%");

		HorizontalPanel basePanel = new HorizontalPanel();
		basePanel.add(flexTable);
		RootPanel.get().add(basePanel);
		
		// left side
		
		VerticalPanel leftSide = new VerticalPanel();
		flexTable.setWidget(0, 0, leftSide);
				
		HorizontalPanel buttonBarTop = new HorizontalPanel();
		leftSide.add(buttonBarTop);
		
		final TextBox tLogin = new TextBox();
		tLogin.setText(default_username);
		leftSide.add(tLogin);

		final Button bConnect = new Button("connect"); 
		buttonBarTop.add(bConnect);
		
		Button bOpen = new Button("open");
		buttonBarTop.add(bOpen);
		
		Button bNew = new Button("new");
		buttonBarTop.add(bNew);
		
//	    final ListBox lWaves = new ListBox();
	    final DCListBox lWaves = new DCListBox();
		lWaves.setWidth("250px");
	    leftSide.add(lWaves);
	    lWaves.setVisibleItemCount(10);
	    
	    HorizontalPanel buttonBarDown = new HorizontalPanel();
	    leftSide.add(buttonBarDown);
	    
	    Button bLog = new Button("log");
	    bLog.setEnabled(false);
	    buttonBarDown.add(bLog);
		
		Button bClearlog = new Button("clearlog");
		buttonBarDown.add(bClearlog);
		
		final Label lLoginStatus = new Label("Login Status: offline");
		leftSide.add(lLoginStatus);
		
		// right side
		
		HorizontalPanel rightSide = new HorizontalPanel();
		flexTable.setWidget(0, 1, rightSide);
		
	    final TextArea tWavecontent = new TextArea();
	    tWavecontent.setCharacterWidth(120);
	    tWavecontent.setVisibleLines(15);
	    rightSide.add(tWavecontent);

		// define actionListener
		
		ClickHandler chOpen = new ClickHandler() {
		      public void onClick(ClickEvent event) {
//		    	  if (lWaves.getItemCount() == 0) {
//		    		  Window.alert("Login is: " + tLogin.getText());
//		    	  } else {
//		    		  String waveId = lWaves.getItemText(lWaves.getSelectedIndex());
//		    		  		    		  
//		    	  }
		        }
		      };
		bOpen.addClickHandler(chOpen);
		
		ClickHandler chConnect = new ClickHandler() {
			public void onClick(ClickEvent event) {

				if ("connect".equals(bConnect.getText())) {
					
					greetingService.serverConnect(tLogin.getText(), new GenericAsyncCallback<Boolean>() {
						public void onSuccess(Boolean result) {
							if (Boolean.TRUE.equals(result)) {
								bConnect.setText("disconnect");
								lLoginStatus.setText("Login Status: online (" + tLogin.getText() + ")");
								
								greetingService.waveListing(new GenericAsyncCallback<String[]>() {
									public void onSuccess(String[] result) {
										if (result == null) {
											// nothing
										} else {
											for (String element : result) {
												lWaves.addItem(element);
											}
										}
									}
								});
								
							} else {
								// nothing happens on errors
							}
							
						}
					});
				
				} else if ("disconnect".equals(bConnect.getText())) {
					
					greetingService.serverDisconnect(new GenericAsyncCallback<Boolean>() {
						public void onSuccess(Boolean result) {
							if (Boolean.TRUE.equals(result)) {
								bConnect.setText("connect");
								lLoginStatus.setText("Login Status: offline");
								
								// clear wave listing
								lWaves.clear();
								
								// clear wave view
								tWavecontent.setText("");
								
							} else {
								// nothing happens on errors
							}
						}
					});
					
				} else {
					// nothing, will never happen
				}
			}
		};
	    bConnect.addClickHandler(chConnect);
	    
		ClickHandler chNew = new ClickHandler() {
		      public void onClick(ClickEvent event) {
		          Window.alert("starting new wave");
		          
					greetingService.waveNew(tLogin.getText(), new GenericAsyncCallback<String>() {
						public void onSuccess(String result) {
							Window.alert("result: " + result);
							
						}
					});
		          
		          Window.alert("wave id is (TODO)");
		        }
	      };
	    bNew.addClickHandler(chNew);

	    DoubleClickHandler chWaves = new DoubleClickHandler() {
			public void onDoubleClick(DoubleClickEvent event) {
				tWavecontent.setText(lWaves.getItemText(lWaves.getSelectedIndex()));
			}
	    };
	    lWaves.addDoubleClickHandler(chWaves);
	    
	    ClickHandler chClearlog = new ClickHandler() {
		      public void onClick(ClickEvent event) {
					tWavecontent.setText("");
		        }	
	    };
	    bClearlog.addClickHandler(chClearlog);

	    // set timer
	    
	    Timer t = new Timer() {
	    	public void run() {
	    		greetingService.waveGetMessage(null, new GenericAsyncCallback<String>() {
					public void onSuccess(String result) {
						if (result == null || result.length() == 0) {
							// nothing
						} else {
							
							
							tWavecontent.setText(result); // + "\n\n" + tWavecontent.getText());
						}


						
					}
				});

	    	}
	    };
//	    t.schedule(500);
	    t.scheduleRepeating(500); 
	    
	}
}
